using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;

using UMEVOR.DTO;

using Microsoft.Practices.EnterpriseLibrary.Data;

namespace UMEVOR.DAL
{
    public class DALT_HIST_NOTICIA : DALConnection
    {

        #region Incluir

        public void incluir(T_HIST_NOTICIA obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" INSERT INTO T_HIST_NOTICIA ");
                SQL.Append("    (DT_ACESSO, ID_NOTICIA) ");
                SQL.Append(" VALUES ");
                SQL.Append("    (str_to_date('" + obj.DT_ACESSO.ToString("dd/MM/yyyy HH:mm:ss") + "', '%d/%m/%Y  %H:%i:%s'), " + obj.ID_NOTICIA.ToString() + ") ");

                DbCommand insertT_HIST_NOTICIA = database.GetSqlStringCommand(SQL.ToString());

                insertT_HIST_NOTICIA.CommandText = SQL.ToString();

                database.ExecuteNonQuery(insertT_HIST_NOTICIA, DALConnection.transaction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Incluir[]

        public void incluir(T_HIST_NOTICIA[] obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();
                DbCommand[] insertT_HIST_NOTICIA = new DbCommand[obj.Length];

                for (int index = 0; index < obj.Length; index++)
                {
                    T_HIST_NOTICIA o = obj[index];
                    SQL = new StringBuilder();

                    SQL.Append(" INSERT INTO T_HIST_NOTICIA ");
                    SQL.Append("    (DT_ACESSO, ID_NOTICIA) ");
                    SQL.Append(" VALUES ");
                    SQL.Append("    (:DT_ACESSO, :ID_NOTICIA) ");

                    insertT_HIST_NOTICIA[index] = database.GetSqlStringCommand(SQL.ToString());

                    database.AddInParameter(insertT_HIST_NOTICIA[index], ":DT_ACESSO", DbType.Object, o.DT_ACESSO);
                    database.AddInParameter(insertT_HIST_NOTICIA[index], ":ID_NOTICIA", DbType.Int64, o.ID_NOTICIA);

                    insertT_HIST_NOTICIA[index].CommandText = SQL.ToString();
                }

                try
                {
                    foreach (DbCommand cmd in insertT_HIST_NOTICIA)
                    {
                        database.ExecuteNonQuery(cmd, DALConnection.transaction);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Alterar

        public void alterar(T_HIST_NOTICIA obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" UPDATE T_HIST_NOTICIA SET ");
                SQL.Append(" WHERE ");
                SQL.Append("     DT_ACESSO = :DT_ACESSO  AND ID_NOTICIA = :ID_NOTICIA  ");

                DbCommand updateT_HIST_NOTICIA = database.GetSqlStringCommand(SQL.ToString());

                database.AddInParameter(updateT_HIST_NOTICIA, ":DT_ACESSO", DbType.Object, obj.DT_ACESSO);
                database.AddInParameter(updateT_HIST_NOTICIA, ":ID_NOTICIA", DbType.Int64, obj.ID_NOTICIA);

                updateT_HIST_NOTICIA.CommandText = SQL.ToString();

                database.ExecuteNonQuery(updateT_HIST_NOTICIA, DALConnection.transaction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Alterar[]

        public void alterar(T_HIST_NOTICIA[] obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();
                DbCommand[] updateT_HIST_NOTICIA = new DbCommand[obj.Length];

                for (int index = 0; index < obj.Length; index++)
                {
                    T_HIST_NOTICIA o = obj[index];
                    SQL = new StringBuilder();
                    SQL.Append(" UPDATE T_HIST_NOTICIA SET ");
                    SQL.Append(" WHERE ");
                    SQL.Append("     DT_ACESSO = :DT_ACESSO  AND ID_NOTICIA = :ID_NOTICIA  ");

                    updateT_HIST_NOTICIA[index] = database.GetSqlStringCommand(SQL.ToString());

                    database.AddInParameter(updateT_HIST_NOTICIA[index], ":DT_ACESSO", DbType.Object, o.DT_ACESSO);
                    database.AddInParameter(updateT_HIST_NOTICIA[index], ":ID_NOTICIA", DbType.Int64, o.ID_NOTICIA);

                    updateT_HIST_NOTICIA[index].CommandText = SQL.ToString();
                }

                try
                {
                    foreach (DbCommand cmd in updateT_HIST_NOTICIA)
                    {
                        database.ExecuteNonQuery(cmd, DALConnection.transaction);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Excluir

        public void excluir(T_HIST_NOTICIA obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" DELETE FROM T_HIST_NOTICIA ");
                SQL.Append(" WHERE ");
                SQL.Append("     DT_ACESSO = :DT_ACESSO  AND ID_NOTICIA = :ID_NOTICIA  ");

                DbCommand deleteT_HIST_NOTICIA = database.GetSqlStringCommand(SQL.ToString());

                database.AddInParameter(deleteT_HIST_NOTICIA, ":DT_ACESSO", DbType.Object, obj.DT_ACESSO);
                database.AddInParameter(deleteT_HIST_NOTICIA, ":ID_NOTICIA", DbType.Int64, obj.ID_NOTICIA);

                deleteT_HIST_NOTICIA.CommandText = SQL.ToString();

                database.ExecuteNonQuery(deleteT_HIST_NOTICIA, DALConnection.transaction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Excluir[]

        public void excluir(T_HIST_NOTICIA[] obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();
                DbCommand[] deleteT_HIST_NOTICIA = new DbCommand[obj.Length];

                for (int index = 0; index < obj.Length; index++)
                {
                    T_HIST_NOTICIA o = obj[index];
                    SQL = new StringBuilder();
                    SQL.Append(" DELETE FROM T_HIST_NOTICIA ");
                    SQL.Append(" WHERE ");
                    SQL.Append("     DT_ACESSO = :DT_ACESSO  AND ID_NOTICIA = :ID_NOTICIA  ");

                    deleteT_HIST_NOTICIA[index] = database.GetSqlStringCommand(SQL.ToString());

                    database.AddInParameter(deleteT_HIST_NOTICIA[index], ":DT_ACESSO", DbType.Object, o.DT_ACESSO);
                    database.AddInParameter(deleteT_HIST_NOTICIA[index], ":ID_NOTICIA", DbType.Int64, o.ID_NOTICIA);

                    deleteT_HIST_NOTICIA[index].CommandText = SQL.ToString();
                }

                try
                {
                    foreach (DbCommand cmd in deleteT_HIST_NOTICIA)
                    {
                        database.ExecuteNonQuery(cmd, DALConnection.transaction);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region ObterPorPK

        public T_HIST_NOTICIA obterPorPK(object pDT_ACESSO, long pID_NOTICIA)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" SELECT * FROM T_HIST_NOTICIA ");
                SQL.Append(" WHERE ");
                SQL.Append("  DT_ACESSO = :DT_ACESSO  AND ID_NOTICIA = :ID_NOTICIA  ");

                DbCommand consultaT_HIST_NOTICIA = database.GetSqlStringCommand(SQL.ToString());
                consultaT_HIST_NOTICIA.CommandText = SQL.ToString();

                database.AddInParameter(consultaT_HIST_NOTICIA, ":DT_ACESSO", DbType.Object, pDT_ACESSO);
                database.AddInParameter(consultaT_HIST_NOTICIA, ":ID_NOTICIA", DbType.Int64, pID_NOTICIA);

                IDataReader reader = database.ExecuteReader(consultaT_HIST_NOTICIA, DALConnection.transaction);
                T_HIST_NOTICIA obj = null;

                if (reader.Read())
                {
                    obj = new T_HIST_NOTICIA();
                    if (!reader.IsDBNull(reader.GetOrdinal("DT_ACESSO")))
                    {
                        obj.DT_ACESSO = DateTime.Parse(reader["DT_ACESSO"].ToString());
                    }
                    if (!reader.IsDBNull(reader.GetOrdinal("ID_NOTICIA")))
                    {
                        obj.ID_NOTICIA = Int64.Parse(reader["ID_NOTICIA"].ToString());
                    }
                }
                reader.Dispose();
                consultaT_HIST_NOTICIA.Dispose();

                return obj;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region ListarTodos

        public List<T_HIST_NOTICIA> listarTodos()
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" SELECT * FROM T_HIST_NOTICIA ");

                DbCommand consultaT_HIST_NOTICIA = database.GetSqlStringCommand(SQL.ToString());
                consultaT_HIST_NOTICIA.CommandText = SQL.ToString();

                IDataReader reader = database.ExecuteReader(consultaT_HIST_NOTICIA, DALConnection.transaction);
                List<T_HIST_NOTICIA> lista = new List<T_HIST_NOTICIA>();
                T_HIST_NOTICIA obj = null;

                while (reader.Read())
                {
                    obj = new T_HIST_NOTICIA();
                    if (!reader.IsDBNull(reader.GetOrdinal("DT_ACESSO")))
                    {
                        obj.DT_ACESSO = DateTime.Parse(reader["DT_ACESSO"].ToString());
                    }
                    if (!reader.IsDBNull(reader.GetOrdinal("ID_NOTICIA")))
                    {
                        obj.ID_NOTICIA = Int64.Parse(reader["ID_NOTICIA"].ToString());
                    }
                    lista.Add(obj);
                }
                reader.Dispose();
                consultaT_HIST_NOTICIA.Dispose();

                return lista;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<VOT_HIST_NOTICIA> consultar(DateTime inicio, DateTime fim)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append("SELECT A.TX_TITULO AS TX_NOTICIA, COUNT(A.ID_NOTICIA) AS QT_NOTICIA FROM T_NOTICIA A, T_HIST_NOTICIA H ");
                SQL.Append("WHERE A.ID_NOTICIA = H.ID_NOTICIA AND H.DT_ACESSO BETWEEN str_to_date('" + inicio.ToString("dd/MM/yyy HH:mm") + "','%d/%m/%Y %H:%i') AND ");
                SQL.Append("str_to_date('" + fim.ToString("dd/MM/yyy HH:mm") + "','%d/%m/%Y %H:%i') GROUP BY A.TX_TITULO ORDER BY A.TX_TITULO");

                DbCommand consultaT_HIST_NOTICIA = database.GetSqlStringCommand(SQL.ToString());
                consultaT_HIST_NOTICIA.CommandText = SQL.ToString();

                IDataReader reader = database.ExecuteReader(consultaT_HIST_NOTICIA, DALConnection.transaction);
                List<VOT_HIST_NOTICIA> lista = new List<VOT_HIST_NOTICIA>();
                VOT_HIST_NOTICIA obj = null;

                while (reader.Read())
                {
                    obj = new VOT_HIST_NOTICIA();
                    if (!reader.IsDBNull(reader.GetOrdinal("TX_NOTICIA")))
                    {
                        obj.TX_NOTICIA = reader["TX_NOTICIA"].ToString();
                    }
                    if (!reader.IsDBNull(reader.GetOrdinal("QT_NOTICIA")))
                    {
                        obj.QT_NOTICIA = Int32.Parse(reader["QT_NOTICIA"].ToString());
                    }
                    lista.Add(obj);
                }
                reader.Dispose();
                consultaT_HIST_NOTICIA.Dispose();

                return lista;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

    }
}

